﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using ZH.Infrastructure.Dto.Request.Login;
using ZH.Infrastructure.Dto.Response.admin;
using ZH.Infrastructure.Dto.Response;
using ZH.Infrastructure.Tools;
using ZH.Service.Interface;
using ZH.Service.Instance;
using ZH.Infrastructure.Dto.Request.Tech;
using ZH.Infrastructure.Dto.Request.Yuyue;
using ZH.Infrastructure.EFCore.MySql;
using Mysqlx.Crud;

namespace ZH.Api.Admin.Controllers.Mobile
{
    /// <summary>
    /// 移动端预约
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Roles = "Mobile")]
    public class AppointmentController : Controller
    {
        public readonly I_Mobile_Service _mobile_Service;
        public readonly I_Yuyue_Service _i_yuyue_Service;

        public AppointmentController(I_Yuyue_Service i_Yuyue_Service, I_Mobile_Service mobile_Service)
        {
            _i_yuyue_Service = i_Yuyue_Service;
            _mobile_Service = mobile_Service;
        }

        /// <summary>
        /// 新增预约
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ADD_App(Yuyue_Request_Dto dto)
        {
            var code = Response.HttpContext.User.Identity?.Name;
            var yy = _mobile_Service.Check_Login(code, "");

            Appointment App = new Appointment();
            App.Aid = Config.GUID();
            App.CustomerName = yy.name;
            App.CustomerPhone = yy.account;
            App.MId = yy.id;
            App.AppointmentDate =dto.Appointment_date;
            App.StartTime = dto.Start_time;
         
            App.ServiceItem = dto.service_item;
            App.ServiceId = dto.service_id;
            App.Notes = dto.notes;
            App.CreatedAt = DateTime.Now;
            App.UpdatedAt = DateTime.Now;
            var result = await _i_yuyue_Service.ADD_App(App);
            return Ok(result);
        }

        /// <summary>
        /// 获取服务类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public async Task<IActionResult> Get_Service_Type()
        {
            var result = await _i_yuyue_Service.Get_Service_Type();
            return Ok(result);
        }


        /// <summary>
        /// 获取服务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Service(int? page = 1, int? limit = 5, string? id = "103")
        {
            var result = await _i_yuyue_Service.Get_Service(page, limit, id);
            return Ok(result);
        }


        /// <summary>
        /// 获取预约记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_order(string? type)
        {

            var code = Response.HttpContext.User.Identity?.Name;
            var user = _mobile_Service.Check_Login(code, "");
            var result = await _i_yuyue_Service.Get_order(user, type);
            return Ok(result);
        }


        /// <summary>
        /// 取消预约
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Cancel_order(Yuyue_Request_Dto dto) 
        {
            var result = await _i_yuyue_Service.Cancel_order(dto);
            return Ok(result);
               
        }

    }
}
